<?php
/*
 * Deposit Payment Plugin Class
 */
class RM_Plugin_Deposit extends RM_Payments_Plugin implements RM_Deposit_Plugin_Interface
{    
    var $name = 'Deposit';

    function calculate($totalPrice) {
        $model = new RM_Deposit();
        $settings = $model->getSettings();
        return $settings->calculate($totalPrice);        
    }

    public function dontUseDepositFor($bookingRef){        
        if (isset($_SESSION['RM_Deposit']) && in_array($bookingRef, $_SESSION['RM_Deposit'])) {
            unset($_SESSION['RM_Deposit'][array_search($bookingRef, $_SESSION['RM_Deposit'])]);
        }
    }

    public function useDepositFor($bookingRef){
        if (isset($_SESSION['RM_Deposit']) == false) {
            $_SESSION['RM_Deposit'] = array();
        }
        $_SESSION['RM_Deposit'][] = $bookingRef;
    }

    public function isDepositUsedFor($bookingRef){
        $model = new RM_Deposit();
        $settings = $model->getSettings();
        if ($settings->enabled == 0) return false;
        if ($settings->force == 1) return true;

        if (isset($_SESSION['RM_Deposit']) == false) {
            return false;
        }
        return in_array($bookingRef, $_SESSION['RM_Deposit']);
    }

    public function getNode(){
        return null;
    }

    /**
     * get the deposit config
     *
     * @return   array
     */
    private function _getConfig()
    {
        $depositObj = new RM_Deposit;
        $value = $depositObj->getSettings()->toArray();
        return $value[0];
    }
}